package cc.comac.saic.hkhy.jsrj.gj.inttools.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cc.comac.saic.hkhy.jsrj.gj.common.mybatis.core.page.PageQuery;
import cc.comac.saic.hkhy.jsrj.gj.inttools.constant.FileType;
import cc.comac.saic.hkhy.jsrj.gj.inttools.mapper.IntToolDataInfoMapper;
import cc.comac.saic.hkhy.jsrj.gj.inttools.mapper.IntToolDataRequirementMapper;
import cc.comac.saic.hkhy.jsrj.gj.inttools.pojo.entity.IntToolDataInfo;
import cc.comac.saic.hkhy.jsrj.gj.inttools.pojo.entity.IntToolDataRequirement;
import cc.comac.saic.hkhy.jsrj.gj.inttools.pojo.vo.RequirementSearchVo;
import cc.comac.saic.hkhy.jsrj.gj.inttools.pojo.vo.RequirementShowVo;
import cc.comac.saic.hkhy.jsrj.gj.inttools.service.IntToolDataRequirementService;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 工具链数据关联需求(IntToolDataRequirement)表服务实现类
 *
 * @author makejava
 * @since 2025-11-18 17:13:07
 */
@Service
public class IntToolDataRequirementServiceImpl extends ServiceImpl<IntToolDataRequirementMapper, IntToolDataRequirement> implements IntToolDataRequirementService {
    @Autowired
    private IntToolDataInfoMapper intToolDataInfoMapper;

    @Override
    public Page<RequirementShowVo> pageByCondition(RequirementSearchVo requirementSearchVo, PageQuery pageQuery) {
        IPage<String> requirementItemIds = this.getBaseMapper().pageItemIdByCondition(pageQuery.build(), requirementSearchVo);

        Page<RequirementShowVo> page = new Page<>();
        BeanUtil.copyProperties(requirementItemIds,page);
        page.setRecords(requirementItemIds.getRecords().stream().map(this::requirementItemId2RequirementShowVo).collect(Collectors.toList()));

        return page;
    }

    private RequirementShowVo requirementItemId2RequirementShowVo(String requirementItemId) {
        RequirementShowVo requirementShowVo = new RequirementShowVo();
        RequirementSearchVo requirementSearchVo = new RequirementSearchVo();
        requirementSearchVo.setRequirementItemId(requirementItemId);
        List<IntToolDataRequirement> intToolDataRequirements = this.getBaseMapper().listNewestByCondition(requirementSearchVo);
        Map<String, List<IntToolDataInfo>> type2IntToolDataInfos = intToolDataRequirements.stream()
            .map(IntToolDataRequirement::getIntToolDataId)
            .map(aLong -> intToolDataInfoMapper.selectById(aLong))
            .collect(Collectors.groupingBy(IntToolDataInfo::getType));

        requirementShowVo.setRequirementItemId(requirementItemId);

        requirementShowVo.setHlrModel(type2IntToolDataInfos.get(FileType.HLR_MODEL.getCode()));
        requirementShowVo.setHlrConsistency(type2IntToolDataInfos.get(FileType.HLR_CONSISTENCY.getCode()));
        requirementShowVo.setArchiModel(type2IntToolDataInfos.get(FileType.ARCHI_MODEL.getCode()));
        requirementShowVo.setControlModel(type2IntToolDataInfos.get(FileType.CONTROL_MODEL.getCode()));
        requirementShowVo.setDisplayModel(type2IntToolDataInfos.get(FileType.DISPLAY_MODEL.getCode()));
        requirementShowVo.setControlCode(type2IntToolDataInfos.get(FileType.CONTROL_CODE.getCode()));
        requirementShowVo.setDisplayCode(type2IntToolDataInfos.get(FileType.DISPLAY_CODE.getCode()));
        requirementShowVo.setImplCode(type2IntToolDataInfos.get(FileType.IMPL_CODE.getCode()));
        requirementShowVo.setTestCase(type2IntToolDataInfos.get(FileType.TEST_CASE.getCode()));
        requirementShowVo.setStaticReport(type2IntToolDataInfos.get(FileType.STATIC_REPORT.getCode()));
        requirementShowVo.setInstrCode(type2IntToolDataInfos.get(FileType.INSTR_CODE.getCode()));
        requirementShowVo.setTestReport(type2IntToolDataInfos.get(FileType.TEST_REPORT.getCode()));

        return requirementShowVo;
    }
}

